package com.aispeech.asr.webase.logger;

import com.aispeech.asr.comm.data.pojo.TaskDoc;
import com.aispeech.asr.comm.data.vo.Result;
import com.aispeech.asr.comm.data.vo.sentence.AsrData;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class BigDataLogger {
    private final static String NODE_NAME = System.getenv().getOrDefault(
            "NODENAME",
            System.getenv().getOrDefault("HOSTNAME", "default")
    );
    private final static String ENV = System.getenv().getOrDefault("ENV", "dev");
    private final static int LOG_ID = 147;
    private final static String PROJECT = "lasr";
    protected final static ObjectMapper objectMapper = new ObjectMapper();

    public static void logTask(TaskDoc task) {
        String taskId = task.getId();
        String productId = task.getProductId();

        List<Map<String, Object>> results = task.getResult();
        Map<String, Object> request = task.getRequest();
        if (results != null) {
            for (int i = 0; i < results.size(); i++) {
                Map<String, Object> item = results.get(i);
                item.put("filepath", String.format("/audios/task_%s/wav/%d.wav", taskId, i + 1));
            }
        }

        Map<String, Object> data = new HashMap<String, Object>(){{
            put("eventName", "TASK");
            put("logId", LOG_ID);
            put("env", ENV);
            put("project", PROJECT);
            put("module", "OFFLINE");
            put("moduleVersion", "0.6.2");
            put("protoVersion", "0.6.2");
            put("logType", "json");
            put("timestamp", System.currentTimeMillis() / 1000);
            put("level", "INFO");
            put("instanceId", NODE_NAME);
            put("userId", "0");
            put("recordId", taskId);
            put("sessionId", taskId);
            put("productId", productId);
            put("audioUrl", String.format("/audios/task_%s/origin.%s", taskId, ((String)request.getOrDefault("audioType", "wav")).toLowerCase()));
            put("message", task);
        }};

        try {
            log.info(String.format("LOGBUSSTART|%s|LOGBUSEND", objectMapper.writeValueAsString(data)));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

    public static void logSentenceResult(Result result, String res, String audioUrl) {
        logSentence(result, (AsrData) result.getData(), res, audioUrl);

    }

    public static void logSentenceError(int errno, String error, AsrData asrData) {
        logSentence(Result.ok(errno, error, asrData), asrData, null, "");
    }

    public static void logSentence(Result result, AsrData asrData, String res, String audioUrl) {
        String sessionId = asrData.getSessionId();
        String productId = asrData.getRequest().getProductId();

        Map<String, Object> data = new HashMap<String, Object>(){{
            put("eventName", "SENTENCE");
            put("logId", LOG_ID);
            put("env", ENV);
            put("project", PROJECT);
            put("module", "SENTENCE");
            put("moduleVersion", "0.6.2");
            put("protoVersion", "0.6.2");
            put("logType", "json");
            put("timestamp", System.currentTimeMillis() / 1000);
            put("level", "INFO");
            put("instanceId", NODE_NAME);
            put("userId", "0");
            put("res", res == null ? "" : res);
            put("recordId", sessionId);
            put("sessionId", sessionId);
            put("productId", productId);
            put("audioUrl", audioUrl);
            put("message", result);
        }};

        try {
            log.info(String.format("LOGBUSSTART|%s|LOGBUSEND", objectMapper.writeValueAsString(data)));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }
}
